//
//  MYGMedicalToolsCRUSADEController.swift
//  MYDoctor
//
//  Created by 林矗 on 2024/5/27.
//

import UIKit

class MYGMedicalToolsCRUSADEController: MYGBaseController {
    // MARK: - Property
    private var ratio: Int?
    private var crea: Int?
    private var heart: Int?
    private var sex: Bool?
    private var body: Bool?
    private var ill: Bool?
    private var sugarIll: Bool?
    private var pressure: Int?

    override func viewDidLoad() {
        super.viewDidLoad()
        setUpUI()
    }
    
  
    private func setUpUI() {
        view.backgroundColor = .white
        view.addSubviews([
            scrollView,
            commitButton
        ])
        
        scrollView.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.bottom.equalTo(commitButton.snp.top)
        }
        
        commitButton.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.height.equalTo(49)
            make.bottom.equalToSuperview().offset(-bottomSafeheight)
        }
        
        scrollView.addSubview(scrollContentView)
        scrollContentView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
            make.width.equalToSuperview()
        }
        
        let line1 = createSeparator()
        let line2 = createSeparator()
        let line3 = createSeparator()
        let line4 = createSeparator()
        let line5 = createSeparator()
        let line6 = createSeparator()
        let line7 = createSeparator()
        let line8 = createSeparator()

        scrollContentView.addSubviews([
            chooseView1,
            line1,
            chooseView2,
            line2,
            chooseView3,
            line3,
            chooseView4,
            line4,
            chooseView5,
            line5,
            chooseView6,
            line6,
            chooseView7,
            line7,
            chooseView8,
            line8,
            scoreView
        ])
        
        chooseView1.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.height.equalTo(56)
            make.left.right.equalToSuperview().inset(26)
        }
        
        line1.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(26)
            make.bottom.equalTo(chooseView1.snp.bottom)
            make.height.equalTo(0.5)
        }
        
        chooseView2.snp.makeConstraints { make in
            make.top.equalTo(chooseView1.snp.bottom)
            make.height.equalTo(56)
            make.left.right.equalToSuperview().inset(26)
        }
        
        line2.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(26)
            make.bottom.equalTo(chooseView2.snp.bottom)
            make.height.equalTo(0.5)
        }
        
        chooseView3.snp.makeConstraints { make in
            make.top.equalTo(chooseView2.snp.bottom)
            make.height.equalTo(56)
            make.left.right.equalToSuperview().inset(26)
        }
        
        line3.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(26)
            make.bottom.equalTo(chooseView3.snp.bottom)
            make.height.equalTo(0.5)
        }
        
        chooseView4.snp.makeConstraints { make in
            make.top.equalTo(chooseView3.snp.bottom)
            make.height.equalTo(56)
            make.left.right.equalToSuperview().inset(26)
        }
        
        line4.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(26)
            make.bottom.equalTo(chooseView4.snp.bottom)
            make.height.equalTo(0.5)
        }
        
        chooseView5.snp.makeConstraints { make in
            make.top.equalTo(chooseView4.snp.bottom)
            make.height.equalTo(56)
            make.left.right.equalToSuperview().inset(26)
        }
        
        line5.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(26)
            make.bottom.equalTo(chooseView5.snp.bottom)
            make.height.equalTo(0.5)
        }
        
        chooseView6.snp.makeConstraints { make in
            make.top.equalTo(chooseView5.snp.bottom)
            make.height.equalTo(56)
            make.left.right.equalToSuperview().inset(26)
        }
        
        line6.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(26)
            make.bottom.equalTo(chooseView6.snp.bottom)
            make.height.equalTo(0.5)
        }
        
        chooseView7.snp.makeConstraints { make in
            make.top.equalTo(chooseView6.snp.bottom)
            make.height.equalTo(56)
            make.left.right.equalToSuperview().inset(26)
        }
        
        line7.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(26)
            make.bottom.equalTo(chooseView7.snp.bottom)
            make.height.equalTo(0.5)
        }
        
        chooseView8.snp.makeConstraints { make in
            make.top.equalTo(chooseView7.snp.bottom)
            make.height.equalTo(56)
            make.left.right.equalToSuperview().inset(26)
        }
        
        line8.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(26)
            make.bottom.equalTo(chooseView8.snp.bottom)
            make.height.equalTo(0.5)
        }
        
        scoreView.snp.makeConstraints { make in
            make.top.equalTo(chooseView8.snp.bottom)
            make.height.equalTo(108)
            make.left.right.equalToSuperview()
            make.bottom.equalToSuperview()
        }
        
    }
    
    func createSeparator() -> UIView {
        let view = UIView()
        view.backgroundColor = .rgba(238, 238, 238)
        return view
    }
    
    private lazy var scrollView: UIScrollView = {
        let scrollView = UIScrollView()
        scrollView.showsVerticalScrollIndicator = false
        scrollView.showsHorizontalScrollIndicator = false
        return scrollView
    }()

    private lazy var scrollContentView: UIView = {
        let view = UIView()
        view.backgroundColor = .white
        return view
    }()
    
    private lazy var chooseView1: MYGMedicalToolsSheetChooseView = {
        let view = MYGMedicalToolsSheetChooseView()
        view.title = "红血球容积比%"
        view.items = ["<31", "31-33.9", "34-36.9", "37-39.9", "≥40"]
        view.action = {[weak self] (index) in
            self?.ratio = index
        }
        return view
    }()
    
    private lazy var chooseView2: MYGMedicalToolsSheetChooseView = {
        let view = MYGMedicalToolsSheetChooseView()
        view.title = "肌酐除去率（mL/min）"
        view.items = ["≤15", "15-30", "30-60", "60-90", "90-120", ">120"]
        view.action = {[weak self] (index) in
            self?.crea = index
        }
        return view
    }()
    
    private lazy var chooseView3: MYGMedicalToolsSheetChooseView = {
        let view = MYGMedicalToolsSheetChooseView()
        view.title = "心率（bpm）"
        view.items = ["≤70", "71-80", "81-90", "91-100", "101-110", "111-120","≥121"]
        view.action = {[weak self] (index) in
            self?.heart = index
        }
        return view
    }()
    
    private lazy var chooseView4: MYGMedicalToolsChooseView = {
        let view = MYGMedicalToolsChooseView(frame: .zero)
        view.title = "性别"
        view.leftTitle = "男"
        view.rightTitle = "女"
        view.selectedAction = {[weak self] (race) in
            self?.sex = race == 0
        }
        return view
    }()
    
    private lazy var chooseView5: MYGMedicalToolsChooseView = {
        let view = MYGMedicalToolsChooseView(frame: .zero)
        view.title = "充血性心衰的体征"
        view.leftTitle = "是"
        view.rightTitle = "否"
        view.selectedAction = {[weak self] (race) in
            self?.body = race == 0
        }
        return view
    }()
    
    private lazy var chooseView6: MYGMedicalToolsChooseView = {
        let view = MYGMedicalToolsChooseView(frame: .zero)
        view.title = "既往血管系统疾病史"
        view.leftTitle = "是"
        view.rightTitle = "否"
        view.selectedAction = {[weak self] (race) in
            self?.ill = race == 0
        }
        return view
    }()
    
    private lazy var chooseView7: MYGMedicalToolsChooseView = {
        let view = MYGMedicalToolsChooseView(frame: .zero)
        view.title = "糖尿病"
        view.leftTitle = "是"
        view.rightTitle = "否"
        view.selectedAction = {[weak self] (race) in
            self?.sugarIll = race == 0
        }
        return view
    }()
    
    private lazy var chooseView8: MYGMedicalToolsSheetChooseView = {
        let view = MYGMedicalToolsSheetChooseView()
        view.title = "缩短压（mmHg）"
        view.items = ["≤90", "91-100", "101-120", "121-180", "181-200", "≥201"]
        view.action = {[weak self] (index) in
            self?.pressure = index
        }
        return view
    }()
    
    
    private lazy var scoreView: MYGMedicalToolsScoreView = {
        let view = MYGMedicalToolsScoreView()
        view.level = "正常"
        view.title = "0"
        return view
    }()
    
    private lazy var commitButton: UIButton = {
        let button = UIButton(type: .custom)
        button.backgroundColor = .rgba(151, 179, 255)
        button.setTitle("开始计算", for: .normal)
        button.setTitleColor(.white, for: .normal)
        button.titleLabel?.font = .medium(17)
        button.addTarget(self, action: #selector(commit), for: .touchUpInside)
        return button
    }()
}

extension MYGMedicalToolsCRUSADEController {
    @objc func commit() {
        guard let ratio = ratio, let crea = crea, let heart = heart, let sex = sex, let body = body, let ill = ill, let sugarIll = sugarIll, let pressure = pressure else {
            MMYGHUD.showToast(text: "请先选择所有项")
            return
        }
        
        var score = 0
        if ratio == 0 {
            score = score + 9
        } else if ratio == 1 {
            score = score + 7
        } else if ratio == 2 {
            score = score + 3
        } else if ratio == 3 {
            score = score + 2
        } else {
            score = score + 0
        }
        
        
        if crea == 0 {
            score = score + 39
        } else if crea == 1 {
            score = score + 35
        } else if crea == 2 {
            score = score + 28
        } else if crea == 3 {
            score = score + 17
        } else if crea == 4 {
            score = score + 7
        } else {
            score = score + 0
        }
        
        if heart == 0 {
            score = score + 0
        } else if heart == 1 {
            score = score + 1
        } else if heart == 2 {
            score = score + 3
        } else if heart == 3 {
            score = score + 6
        } else if heart == 4 {
            score = score + 8
        } else if heart == 5 {
            score = score + 10
        } else {
            score = score + 11
        }
        
        if sex {
            score = score + 0
        } else {
            score = score + 8
        }
        
        if body {
            score = score + 7
        }
        
        if ill {
            score = score + 6
        }
        
        if sugarIll {
            score = score + 6
        }
        
        
        if pressure == 0 {
            score = score + 10
        } else if pressure == 1 {
            score = score + 8
        } else if pressure == 2 {
            score = score + 5
        } else if pressure == 3 {
            score = score + 1
        } else if pressure == 4 {
            score = score + 3
        } else {
            score = score + 5
        }
        
       
        if score <= 20 {
            scoreView.level = "极低危"
        } else if score <= 30 {
            scoreView.level = "低危"
        } else if score <= 40 {
            scoreView.level = "中危"
        } else if score <= 50 {
            scoreView.level = "高危"
        } else {
            scoreView.level = "极高危"
        }
        
        scoreView.progress = Double(score) / 96.0
        scoreView.title = "\(score)"
    }
}

